```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深度学习模型推理指南 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', 'Noto Serif SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background-color: #2d3748;
            border-left: 4px solid #667eea;
        }
        .step-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .tooltip:hover .tooltip-text {
            opacity: 1;
            visibility: visible;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4">
        <div class="container mx-auto max-w-6xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-4">深度学习模型推理指南</h1>
                    <p class="text-xl mb-6 opacity-90">使用DJL框架实现高效的图像分类预测</p>
                    <div class="flex items-center">
                        <span class="inline-block bg-white bg-opacity-20 rounded-full px-4 py-2 mr-3">
                            <i class="fas fa-brain mr-2"></i>MLP模型
                        </span>
                        <span class="inline-block bg-white bg-opacity-20 rounded-full px-4 py-2">
                            <i class="fas fa-image mr-2"></i>图像分类
                        </span>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="relative w-64 h-64">
                        <div class="absolute inset-0 bg-white bg-opacity-10 rounded-lg transform rotate-6"></div>
                        <div class="absolute inset-0 bg-white bg-opacity-10 rounded-lg transform -rotate-6"></div>
                        <div class="absolute inset-0 bg-white bg-opacity-10 rounded-lg flex items-center justify-center">
                            <i class="fas fa-project-diagram text-6xl text-white"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Content Section -->
    <main class="container mx-auto max-w-6xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2">模型推理流程概述</h2>
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <p class="text-lg mb-4 text-gray-700 font-medium">在深度学习中，训练一个有效的模型是整个流程中的重要一步。在我们的案例中，假设你已经使用DJL训练了一个简单的多层感知机（MLP）模型。</p>
                    <p class="mb-4">这个模型专门用于识别手写数字（MNIST数据集），能够将28x28像素的图像分类为0-9十个数字类别。</p>
                    <div class="bg-blue-50 border-l-4 border-blue-500 p-4 rounded mb-6">
                        <p class="font-semibold text-blue-800">模型结构说明</p>
                        <ul class="list-disc pl-5 text-blue-700">
                            <li>输入层：784个神经元（28×28像素）</li>
                            <li>隐藏层1：128个神经元</li>
                            <li>隐藏层2：64个神经元</li>
                            <li>输出层：10个神经元（0-9分类）</li>
                        </ul>
                    </div>
                </div>
                <div class="flex items-center justify-center">
                    <div class="mermaid bg-white p-6 rounded-lg shadow">
                        graph TD
                            A[28x28输入图像] --> B[Flatten到784维]
                            B --> C[128神经元隐藏层]
                            C --> D[64神经元隐藏层]
                            D --> E[10神经元输出层]
                            E --> F[0-9分类结果]
                    </div>
                </div>
            </div>
        </section>

        <!-- Step by Step Guide -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-2">逐步实现模型推理</h2>
            
            <!-- Step 1 -->
            <div class="step-card transition-all duration-300 bg-white rounded-lg shadow-md overflow-hidden mb-8">
                <div class="md:flex">
                    <div class="md:w-1/3 bg-blue-100 p-6 flex items-center">
                        <div>
                            <div class="w-12 h-12 bg-blue-600 text-white rounded-full flex items-center justify-center text-xl font-bold mb-3">1</div>
                            <h3 class="text-xl font-bold text-blue-800 mb-2">准备测试数据</h3>
                            <p class="text-blue-700">加载待预测的图像文件</p>
                        </div>
                    </div>
                    <div class="md:w-2/3 p-6">
                        <div class="code-block rounded-md p-4 mb-4 text-gray-100">
                            <code class="block">
                                <span class="text-blue-400">// 从本地文件加载图像</span><br>
                                <span class="text-purple-400">Image</span> image = <span class="text-purple-400">ImageFactory</span>.getInstance().fromFile(<br>
                                &nbsp;&nbsp;&nbsp;&nbsp;<span class="text-purple-400">Paths</span>.get(<span class="text-green-400">"C:\\Users\\toString\\Desktop\\8.png"</span>));<br><br>
                                <span class="text-blue-400">// 或者从URL加载图像</span><br>
                                <span class="text-green-400">// Image image = ImageFactory.getInstance().fromUrl("https://...");</span>
                            </code>
                        </div>
                        <div class="flex items-center text-sm text-gray-600">
                            <i class="fas fa-info-circle mr-2 text-blue-500"></i>
                            <p>支持本地文件路径和网络URL两种方式加载图像</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Step 2 -->
            <div class="step-card transition-all duration-300 bg-white rounded-lg shadow-md overflow-hidden mb-8">
                <div class="md:flex">
                    <div class="md:w-1/3 bg-purple-100 p-6 flex items-center">
                        <div>
                            <div class="w-12 h-12 bg-purple-600 text-white rounded-full flex items-center justify-center text-xl font-bold mb-3">2</div>
                            <h3 class="text-xl font-bold text-purple-800 mb-2">加载训练好的模型</h3>
                            <p class="text-purple-700">从本地目录加载预训练模型</p>
                        </div>
                    </div>
                    <div class="md:w-2/3 p-6">
                        <div class="code-block rounded-md p-4 mb-4 text-gray-100">
                            <code class="block">
                                <span class="text-purple-400">Path</span> modelDir = <span class="text-purple-400">Paths</span>.get(<span class="text-green-400">"build/mlp"</span>);<br>
                                <span class="text-purple-400">Model</span> model = <span class="text-purple-400">Model</span>.newInstance(<span class="text-green-400">"mlp"</span>);<br>
                                model.setBlock(<span class="text-purple-400">new Mlp</span>(<br>
                                &nbsp;&nbsp;&nbsp;&nbsp;<span class="text-yellow-400">28 * 28</span>,   <span class="text-blue-400">// 输入层：28x28像素</span><br>
                                &nbsp;&nbsp;&nbsp;&nbsp;<span class="text-yellow-400">10</span>,        <span class="text-blue-400">// 输出层：10个类别</span><br>
                                &nbsp;&nbsp;&nbsp;&nbsp;<span class="text-purple-400">new int</span>[]{<span class="text-yellow-400">128</span>, <span class="text-yellow-400">64</span>}  <span class="text-blue-400">// 隐藏层结构</span><br>
                                ));<br>
                                model.load(modelDir);  <span class="text-blue-400">// 加载模型文件</span>
                            </code>
                        </div>
                        <div class="bg-purple-50 border-l-4 border-purple-500 p-4 rounded">
                            <p class="font-medium text-purple-800">关键参数说明：</p>
                            <p class="text-purple-700 text-sm">必须确保模型结构与训练时完全一致，包括各层神经元数量。</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Step 3 -->
            <div class="step-card transition-all duration-300 bg-white rounded-lg shadow-md overflow-hidden mb-8">
                <div class="md:flex">
                    <div class="md:w-1/3 bg-green-100 p-6 flex items-center">
                        <div>
                            <div class="w-12 h-12 bg-green-600 text-white rounded-full flex items-center justify-center text-xl font-bold mb-3">3</div>
                            <h3 class="text-xl font-bold text-green-800 mb-2">创建图像翻译器</h3>
                            <p class="text-green-700">预处理图像数据</p>
                        </div>
                    </div>
                    <div class="md:w-2/3 p-6">
                        <div class="code-block rounded-md p-4 mb-4 text-gray-100">
                            <code class="block">
                                <span class="text-purple-400">Translator</span>&lt;<span class="text-purple-400">Image</span>, <span class="text-purple-400">Classifications</span>&gt; translator =<br>
                                &nbsp;&nbsp;&nbsp;&nbsp;<span class="text-purple-400">ImageClassificationTranslator</span>.builder()<br>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.addTransform(<span class="text-purple-400">new Resize</span>(<span class="text-yellow-400">28</span>, <span class="text-yellow-400">28</span>))  <span class="text-blue-400">// 调整大小</span><br>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.addTransform(<span class="text-purple-400">new ToTensor</span>())      <span class="text-blue-400">// 转换为张量</span><br>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.build();
                            </code>
                        </div>
                        <div class="flex items-start">
                            <div class="mr-4">
                                <div class="bg-white border border-gray-200 rounded-lg p-3 text-center w-24">
                                    <i class="fas fa-expand-alt text-green-500 text-2xl mb-2"></i>
                                    <p class="text-xs font-medium text-gray-600">Resize</p>
                                </div>
                            </div>
                            <div>
                                <div class="bg-white border border-gray-200 rounded-lg p-3 text-center w-24">
                                    <i class="fas fa-cube text-green-500 text-2xl mb-2"></i>
                                    <p class="text-xs font-medium text-gray-600">ToTensor</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Step 4 & 5 -->
            <div class="step-card transition-all duration-300 bg-white rounded-lg shadow-md overflow-hidden mb-8">
                <div class="md:flex">
                    <div class="md:w-1/3 bg-yellow-100 p-6 flex items-center">
                        <div>
                            <div class="w-12 h-12 bg-yellow-600 text-white rounded-full flex items-center justify-center text-xl font-bold mb-3">4/5</div>
                            <h3 class="text-xl font-bold text-yellow-800 mb-2">创建预测器并执行预测</h3>
                            <p class="text-yellow-700">获取分类结果</p>
                        </div>
                    </div>
                    <div class="md:w-2/3 p-6">
                        <div class="code-block rounded-md p-4 mb-4 text-gray-100">
                            <code class="block">
                                <span class="text-purple-400">Predictor</span>&lt;<span class="text-purple-400">Image</span>, <span class="text-purple-400">Classifications</span>&gt; predictor = <br>
                                &nbsp;&nbsp;&nbsp;&nbsp;model.newPredictor(translator);<br><br>
                                <span class="text-purple-400">Classifications</span> predict = predictor.predict(image);<br>
                                System.out.println(predict);
                            </code>
                        </div>
                        <div class="bg-yellow-50 border-l-4 border-yellow-500 p-4 rounded">
                            <p class="font-medium text-yellow-800">预测结果示例：</p>
                            <div class="code-block rounded-md p-4 text-gray-100">
                                <code>
                                    [<br>
                                    &nbsp;&nbsp;{"class": "8", "probability": 0.92},<br>
                                    &nbsp;&nbsp;{"class": "3", "probability": 0.05},<br>
                                    &nbsp;&nbsp;{"class": "6", "probability": 0.03}<br>
                                    ]
                                </code>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Complete Code Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2">完整代码实现</h2>
            <div class="bg-white rounded-lg shadow-md overflow-hidden">
                <div class="bg-gray-800 px-4 py-2 flex items-center">
                    <div class="flex space-x-2 mr-4">
                        <div class="w-3 h-3 rounded-full bg-red-500"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500"></div>
                    </div>
                    <span class="text-gray-300 text-sm">ModelInferenceTest.java</span>
                </div>
                <div class="code-block rounded-none p-6 text-gray-100">
                    <code class="block">
<span class="text-blue-400">@Test</span><br>
<span class="text-purple-400">public void</span> <span class="text-yellow-400">testModel</span>() <span class="text-purple-400">throws</span> <span class="text-purple-400">Exception</span> {<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-blue-400">// 1. 准备测试数据</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-purple-400">Image</span> image = <span class="text-purple-400">ImageFactory</span>.getInstance().fromFile(<span class="text-purple-400">Paths</span>.get(<span class="text-green-400">"C:\\Users\\toString\\Desktop\\8.png"</span>));<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-blue-400">// 2. 加载训练好的模型</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-purple-400">Path</span> modelDir = <span class="text-purple-400">Paths</span>.get(<span class="text-green-400">"build/mlp"</span>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-purple-400">Model</span> model = <span class="text-purple-400">Model</span>.newInstance(<span class="text-green-400">"mlp"</span>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.setBlock(<span class="text-purple-400">new Mlp</span>(<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-yellow-400">28 * 28</span>,   <span class="text-blue-400">// 输入层</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-yellow-400">10</span>,        <span class="text-blue-400">// 输出层</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-purple-400">new int</span>[]{<span class="text-yellow-400">128</span>, <span class="text-yellow-400">64</span>}  <span class="text-blue-400">// 隐藏层</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;));<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.load(modelDir);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-blue-400">// 3. 创建图像分类翻译器</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-purple-400">Translator</span>&lt;<span class="text-purple-400">Image</span>, <span class="text-purple-400">Classifications</span>&gt; translator =<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-purple-400">ImageClassificationTranslator</span>.builder()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.addTransform(<span class="text-purple-400">new Resize</span>(<span class="text-yellow-400">28</span>, <span class="text-yellow-400">28</span>))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.addTransform(<span class="text-purple-400">new ToTensor</span>())<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.build();<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-blue-400">// 4. 创建预测器</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-purple-400">Predictor</span>&lt;<span class="text-purple-400">Image</span>, <span class="text-purple-400">Classifications</span>&gt; predictor = model.newPredictor(translator);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-blue-400">// 5. 预测图像类别</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-purple-400">Classifications</span> predict = predictor.predict(image);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="text-blue-400">// 6. 输出预测结果</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(predict);<br>
}
                    </code>
                </div>
            </div>
        </section>

        <!-- Key Concepts -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2">核心概念解析</h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white p-6 rounded-lg shadow-md">
                    <div class="w-12 h-12 bg-blue-100 rounded-full flex items-center justify-center text-blue-600 text-xl mb-4">
                        <i class="fas fa-cubes"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2">模型结构</h3>
                    <p class="text-gray-600">多层感知机(MLP)是一种前馈神经网络，包含输入层、隐藏层和输出层。本案例中使用两个隐藏层(128和64个神经元)来学习图像特征。</p>
                </div>
                <div class="bg-white p-6 rounded-lg shadow-md">
                    <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center text-purple-600 text-xl mb-4">
                        <i class="fas fa-exchange-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2">翻译器(Translator)</h3>
                    <p class="text-gray-600">负责数据预处理和后处理，将原始图像转换为模型可接受的张量格式，并将模型输出转换为可读的分类结果。</p>
                </div>
                <div class="bg-white p-6 rounded-lg shadow-md">
                    <div class="w-12 h-12 bg-green-100 rounded-full flex items-center justify-center text-green-600 text-xl mb-4">
                        <i class="fas fa-chart-line"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2">预测结果</h3>
                    <p class="text-gray-600">Classifications对象包含预测的类别及其概率。概率值最高的类别即为模型的最终预测结果。</p>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-6xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-gray-400">探索技术之美，分享知识之光</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="hover:text-white transition-colors duration-300">
                        <i class="fas fa-external-link-alt mr-2"></i>访问技术小馆语雀
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-sm text-gray-500">
                <p>© 2023 技术小馆. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <!-- Mermaid JS -->
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```